package com.itheima.protocol.curatorMasterSelector;


import com.itheima.protocol.curator.CuratorClientUtils;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.leader.LeaderSelector;
import org.apache.curator.framework.recipes.leader.LeaderSelectorListenerAdapter;

import java.util.concurrent.TimeUnit;

/**
 * curator master selector
 */
public class MasterSelector {

    private static final String MASTER_PATH = "/curator_master";

    public static void main(String[] args) {

        CuratorFramework curatorFramework = CuratorClientUtils.getInstance();

        LeaderSelector leaderSelector = new LeaderSelector(curatorFramework, MASTER_PATH, new LeaderSelectorListenerAdapter() {

            @Override
            public void takeLeadership(CuratorFramework curatorFramework) throws Exception {
                System.out.println("获取leader成功");
                TimeUnit.SECONDS.sleep(2);
            }
        });

        //自动争抢
        leaderSelector.autoRequeue();

        //开始选举
        leaderSelector.start();
    }
}